<?php

namespace App\Models;

/**
 * App\Models\Partner
 *
 * @property int $id
 * @property string $title 名称
 * @property string $thumb 广告图
 * @property string $link 链接
 * @property int $target 新窗口1是2否
 * @property int $status 状态1正常2锁定
 * @property int $sort 排序
 * @method static \Illuminate\Database\Eloquent\Builder|Partner newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Partner newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Partner query()
 * @method static \Illuminate\Database\Eloquent\Builder|Partner whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Partner whereLink($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Partner whereSort($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Partner whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Partner whereTarget($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Partner whereThumb($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Partner whereTitle($value)
 * @mixin \Eloquent
 */
class Partner extends Base
{
    protected $table = 'partners';

    const STATUS_1 = 1;
    const STATUS_2 = 2;

    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag">正常</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--danger">锁定</span>',
    ];

    const TARGET_1 = 1;
    const TARGET_2 = 2;

    const TARGET_TEXT = [
        self::TARGET_1 => '<span class="el-tag  el-tag--danger">是</span>',
        self::TARGET_2 => '<span class="el-tag">否</span>',
    ];


    protected $fillable = [
        'id','title','link','sort','thumb','target','status',
    ];

    public $timestamps = false;//关闭自动维护

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*']){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('title','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['status']) && $params['status']) {
                    $query ->where('status',$params['status']);
                }
            })->orderBy('id', 'desc')
            ->paginate(config('app.limit'));
        $this->stateToText($lists,[
            'status' => self::STATUS_TEXT,
            'target' => self::TARGET_TEXT,
        ]);
        return $lists;
    }

    // 首页3个合作伙伴
    static function getIndex()
    {
        return self::limit(3)->where('status', 1)->get();
    }

}
